using System;
using System.Windows;
using System.Windows.Controls;

namespace Cirrostrata.CycleList
{
    public class CyclePanel : Panel
    {
        protected override Size MeasureOverride(Size availableSize)
        {
            var resultSize = new Size(0, 0);

            foreach (ListBoxItem child in InternalChildren)
            {
                child.Measure(availableSize);

                resultSize.Width = Math.Max(resultSize.Width, child.DesiredSize.Width);
                resultSize.Height = Math.Max(resultSize.Height, child.DesiredSize.Height);
            }

            resultSize.Width = double.IsPositiveInfinity(availableSize.Width) ?
                resultSize.Width : availableSize.Width;

            resultSize.Height = double.IsPositiveInfinity(availableSize.Height) ?
                resultSize.Height : availableSize.Height;

            return resultSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            foreach (ListBoxItem child in InternalChildren)
            {
                if (child.IsSelected)
                {
                    child.Visibility = Visibility.Visible;
                    child.Arrange(new Rect(child.DesiredSize));
                }
                else
                {
                    child.Visibility = Visibility.Hidden;
                    child.Arrange(new Rect(new Size(0,0)));
                }
            }

            return finalSize;
       }
    }
}